local Construct = require'constructs.construct'


local Tridock = {}
Tridock.mt = { __index = Tridock }
setmetatable(Tridock, Construct.mt)

function Tridock.initialize( x, y, w, h )
	local tdock = {}
	
	-- these disallow certain small shapes, most
	-- are useless,  
	-- 3,4 is a two tile airlocked connector and is enabled
	-- 4, 4 has a single airlock on the bottom and two airlocks
	-- 		next to each other on the top, and is diabled
	--if w ~=3 and h ~= 4 then
		--if w < 5 then w = 5 end
		--if h < 6 then h = 6 end
	--end
	
	tdock.x = x
	tdock.y = y
	tdock.w = w
	tdock.h = h
	tdock.parts = {}
	tdock.layout = Tridock.plan_dock( tdock )
	
	
	tdock = Construct.new( tdock.layout, nil, x, y )
	setmetatable(tdock, Tridock.mt)
	
	tdock.x = x
	tdock.y = y
	tdock.w = w
	tdock.h = h
	
	tdock.type = "tridock"
	
	tdock.offset_x = tdock.w*0.3
	tdock.offset_y = tdock.h-2
	
	
	
	
	return tdock

end


function Tridock.new( x, y, w, h )

	local o = Tridock.initialize(x, y, w, h)
	
	
	
	return o

end

function Tridock:plan_dock()
	
	local plan = {}
	
	local mid = math.ceil(self.w*0.5)
	
	local floor_level = 3
	
	for y = 1, self.h do
		plan[y] = {}
		for x = 1, self.w do
			
			if y == 1 then
				
				if x == 2 or x == self.w-1 then
				
					plan[y][x] = HOOK_TILE
					
				else
				
					plan[y][x] = 0
					
				end
			
			end
			
			if y == 2 then
				if x == 2 or x == self.w-1 then
				
					plan[y][x] = AIRLOCK_TILE
				else
					plan[y][x] = WALL_TILE
				end
			
			end
			
			if y == 3 then
				if x == 1 or x == self.w then
				
					plan[y][x] = WALL_TILE
				else
				
					plan[y][x] = FLOOR_TILE
				
				end
			
			end
			
			if y > 3 and y < self.h-1 then
				if y == 4 and x ~= mid then
					plan[y][x] = WALL_TILE
				end
				
				if x == mid then
					plan[y][x] = FLOOR_TILE
				end
				
				if x == mid-1 or x == mid+1 then
					
					plan[y][x] = WALL_TILE
				
				end
			
			end
			
			if y == self.h-1 then
				if x == mid then
					
					plan[y][x] = AIRLOCK_TILE
				
				end
				
				if x == mid-1 or x == mid+1 then
					
					plan[y][x] = WALL_TILE
				
				end
			
			end
			
			if y == self.h then
				if x == mid then
				
					plan[y][x] = HOOK_TILE
					
				end
			
			end		
		end
	
	end
	
	

	return plan
end

function Tridock:hooker( const_a, const_b, const_c )

	local dock_hooks = self:get_hooks()
	local dhx, dhy = dock_hooks[1].prop:getLoc()
	local dhx2, dhy2 = dock_hooks[2].prop:getLoc()
	local dhx3, dhy3 = dock_hooks[3].prop:getLoc()
	
	local const_a_hooks, cahx, cahy, ca, caox, caoy
	
	if const_a then
		const_a_hooks = const_a:get_hooks()
		cahx, cahy = const_a_hooks[1].prop:getLoc()
		ca = const_a_hooks[1]
		caox = const_a.offset_x*TILE_SIZE
		caoy = const_a.offset_y*TILE_SIZE
		const_a.transform:setAttrLink(MOAITransform.INHERIT_TRANSFORM, dock_hooks[1].prop, MOAITransform.TRANSFORM_TRAIT)
	end
	
	
	local const_b_hooks, cbhx, cbhy, cb, cbox, cboy
	
	if const_b then
		const_b_hooks = const_b:get_hooks()
		cbhx, cbhy = const_b_hooks[1].prop:getLoc()
		cb = const_b_hooks[1]
		cbox = const_b.offset_x*TILE_SIZE
		cboy = const_b.offset_y*TILE_SIZE
		const_b.transform:setAttrLink(MOAITransform.INHERIT_TRANSFORM, dock_hooks[2].prop, MOAITransform.TRANSFORM_TRAIT)
	end
	
	local const_c_hooks, cchx, cchy, cc, ccox, ccoy
	
	if const_c then
		const_c_hooks = const_c:get_hooks()
		cchx, cchy = const_c_hooks[1].prop:getLoc()
		cc = const_c_hooks[1]
		ccox = const_c.offset_x*TILE_SIZE
		ccoy = const_c.offset_y*TILE_SIZE
		const_c.transform:setAttrLink(MOAITransform.INHERIT_TRANSFORM, dock_hooks[3].prop, MOAITransform.TRANSFORM_TRAIT)
	end
	
	if const_a then
		const_a:setPiv(dhx, dhy) 
		const_a:seekLoc(-caox, -caoy, const_a.dock_time)
		--const_a:seekLoc(caox, caoy, const_a.dock_time) 	
		--const_a:seekRot(180,  const_a.dock_time) 
	
		--const_a:seekRot(0, const_a.dock_time)
	
	end
	
	if const_b then
		const_b:setPiv(dhx2, dhy2)
	
	
		-- LOOK AT THIS FUCKED UP CALCULATION
		--const_b:seekLoc(-cbox-((self.w-3)*TILE_SIZE), -cboy-(self.h+1)*TILE_SIZE, const_b.dock_time)
		const_b:seekLoc(-cbox+(self.w-3)*TILE_SIZE, -cboy, const_b.dock_time)
	
	end
	
	if const_c then
		const_c:seekRot(180, const_c.dock_time)
		const_c:setPiv(dhx3, dhy3)
		
		const_c:seekLoc(ccox-((math.ceil(self.w*0.5))-2)*TILE_SIZE, ccoy-(self.h-1)*TILE_SIZE, const_c.dock_time )
		
		--const_c:seekLoc(ccox-(self.w*TILE_SIZE)+((self.w-2)*TILE_SIZE), ccoy-(2*TILE_SIZE), const_c.dock_time)
	
	
	end
	
	if not self.docked_const then self.docked_const = {} end
	
	local iter = #self.docked_const+1
	
	if const_a then
		self.docked_const[iter] = const_a
		const_a:toggleDock()
	end
	
	if const_b then
		iter = #self.docked_const+1
		self.docked_const[iter] = const_b
		const_b:toggleDock()
	end
	
	if const_c then
		iter = #self.docked_const+1
		self.docked_const[iter] = const_c
		const_c:toggleDock()
	end
	
end


function Tridock:unhook( const_a, const_b, const_c )
	
	if const_a then
		const_a.transform:clearAttrLink(MOAITransform.INHERIT_TRANSFORM)
		const_a:toggleDock()
	end
	
	if const_b then
		const_b.transform:clearAttrLink(MOAITransform.INHERIT_TRANSFORM)
		const_b:toggleDock()
	end	
	
	if const_c then
		const_c.transform:clearAttrLink(MOAITransform.INHERIT_TRANSFORM)
		const_c:toggleDock()
	end	

end




return Tridock
